package com.woniuxy.rabbitmq79.publish;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.sun.org.apache.bcel.internal.generic.IfInstruction;

/**
 * @Author zhangyong
 * @Date 2021/8/11 0011下午 2:14
 */
public class Productor {
    public static void main(String[] args) throws Exception {
        //1.创建连接工厂：创建连接
        ConnectionFactory factory = new ConnectionFactory();

        //2.设置连接参数
        factory.setHost("localhost");   //mq的ip
        factory.setUsername("guest");   //账号
        factory.setPassword("guest");   //密码
        factory.setVirtualHost("/");    //设置虚拟主机 不设置默认/

        //3.创建连接
        Connection connection = factory.newConnection();     //IO异常

        //4.创建通道
        Channel channel = connection.createChannel();

        //创建交换机
        //参数1：交换机的名字
        //参数2：交换机的类型    fanout用于广播      topic主题
        channel.exchangeDeclare("publish_exchange", "fanout");

        //向交换机发送消息
        for (int i = 0; i < 10; i++) {
            String message = "publish" + i;
            //参数1：交换机名字
            //参数2：路由
            //参数3：元数据
            //参数4：消息
            channel.basicPublish("publish_exchange", "", null, message.getBytes());
        }
        //
        channel.close();
        connection.close();

    }
}
